package com.example.lizh.mvp_example;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lizh.mvp_example.presenter.LoginPresenter;
import com.example.lizh.mvp_example.rxBus.RxBus;
import com.example.lizh.mvp_example.rxBus.event.LoginStoreChangeEvent;
import com.example.lizh.mvp_example.view.LoginView;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import io.reactivex.functions.Consumer;

public class LoginActivity extends RxAppCompatActivity implements LoginView {

    EditText et_name;
    EditText et_password;
    Button btn_login;

    private LoginPresenter mUserLoginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mUserLoginPresenter = new LoginPresenter(this);

        et_name = (EditText) findViewById(R.id.name);
        et_password = (EditText) findViewById(R.id.password);
        btn_login = (Button) findViewById(R.id.login);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserLoginPresenter.login();
            }
        });
    }

    @Override
    public String getName() {
        return et_name.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString().trim();
    }

    @Override
    public void subscribe() {
        // 订阅事件
        RxBus.getInstance()
                .toObservable(LoginStoreChangeEvent.class) // 只接收 LoginStoreChangeEvent 及其子类,不接收其父类或兄弟类等
                /*
                 * this.<TestEvent>bindUntilEvent(FragmentEvent.DESTROY):【手动】手动指定序列结束的时间(Activity使用ActivityEvent,Fragment选择FragmentEvent)
                 * this.<TestEvent>bindToLifecycle():【自动】让RxLifecycle自动在适当的时间结束序列
                 *
                 * PS:1.需要集成Rx系列的Activity或者Fragment
                 *    2.bindXXX()前要加泛型,泛型类型需要和observer的泛型类型保持一致
                 */
                .compose(this.<LoginStoreChangeEvent>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<LoginStoreChangeEvent>() {
                    @Override
                    public void accept(@NonNull LoginStoreChangeEvent loginStoreChangeEvent) throws Exception {
                        String actionResult = loginStoreChangeEvent.getActionResult();
                        // 匹配返回值
                        if ("登录成功".equals(actionResult)) { // 登录成功事件
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        } else if ("登录失败".equals(actionResult)) { // 登录失败事件
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        } else if ("登录请求异常".equals(actionResult)) { // 登录请求异常事件
                            Toast.makeText(LoginActivity.this, "登录请求异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}
